<template>
  <div>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入用户名..." v-model="queryForm.query" clearable ></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initUserList">搜索</el-button>
      <el-button type="success"  @click="handleDialogValue()">新增</el-button>
      <el-popconfirm title="您确定批量删除这些记录吗？" @confirm="handleDelete(null)">
        <template #reference>
         <el-button type="danger" :disabled="delBtnStatus" :icon="Delete" >批量删除</el-button>
        </template>
       </el-popconfirm>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="avatar" label="头像" width="80"">
          <template v-slot="scope">
            <img :src="getServerUrl()+'image/userAvatar/'+scope.row.avatar" width="50" height="50"/>
          </template>
        </el-table-column> -->
        <el-table-column prop="username" label="用户名"  width="100" />
        <el-table-column prop="roles" label="拥有角色" width="200" >
          <template v-slot="scope">
            <el-tag size="small" type="warning" v-for="item in scope.row.sysRoleList">   {{item.name}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="200" />
        <el-table-column prop="phonenumber" label="手机号" width="120"/>
        <el-table-column prop="status" label="状态？" width="200"  >
          <template v-slot="{row}" >
            <el-switch  v-model="row.status" @change="statusChangeHandle(row)" active-text="正常"
                        inactive-text="禁用" active-value="0" inactive-value="1"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="200" />
        <el-table-column prop="loginDate" label="最后登录时间" width="200" />
        <el-table-column prop="action" label="操作" width="400">
        <template v-slot="scope" >
          <el-button  type="primary" :icon="Tools" @click="handleRoleDialogValue(scope.row.id,scope.row.sysRoleList)">分配角色</el-button>
          <el-button v-if="scope.row.username!='java1234'" type="primary" :icon="Edit" @click="handleDialogValue(scope.row.id)" >修改</el-button>
          <el-popconfirm  v-if="scope.row.username!='java1234'" title="您确定要删除这条记录吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button  type="danger" :icon="Delete" >删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:currentPage="queryForm.pageNum"
        v-model:page-size="queryForm.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
  <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle" @initUserList="initUserList"/>
  <RoleDialog v-model="roleDialogVisible" :sysRoleList="sysRoleList" :roleDialogVisible="roleDialogVisible" :id="id" @initUserList="initUserList"></RoleDialog>
</template>

<script setup>
import { ref, reactive, computed } from "vue"
import requestUtil,{getServerUrl} from "@/util/request";
import Dialog from "@/views/sys/user/components/dialog.vue";
import RoleDialog from "@/views/sys/user/components/roleDialog.vue";
import { ElMessage, ElMessageBox } from 'element-plus'

const total = ref(0)

const tableData=ref({})
const queryForm=ref({
  query:'',
  pageNum:1,
  pageSize:10
})
const dialogVisible=ref(false)

const dialogTitle=ref("")

const id=ref(-1)

const delBtnStatus=ref(true)

const multipleSelection=ref([])

const sysRoleList=ref([])

const roleDialogVisible=ref(false)

const handleRoleDialogValue=(userId,roleList)=>{
  id.value=userId;
  sysRoleList.value=roleList;
  roleDialogVisible.value=true
}


const handleSelectionChange=(selection)=>{
  console.log("勾选了")
  console.log(selection)
  multipleSelection.value=selection;
  delBtnStatus.value=selection.length==0;
}
const handleDelete=async (id)=>{
  var ids = []
  if(id){
    ids.push(id)
  }else{
    multipleSelection.value.forEach(row=>{
      ids.push(row.id)
    })
  }
  const res=await requestUtil.post("member/sys/user/delete",ids)
  if(res.data.code==200){
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
    initUserList();
  }else{
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
  }
}



const initUserList=async()=>{
  const res=await requestUtil.post("member/sys/user/list",queryForm.value);
  tableData.value=res.data.userList;
  total.value=res.data.total;
}
initUserList()
const handleSizeChange=(pageSize)=>{
  queryForm.value.pageNum=1;
  queryForm.value.pageSize=pageSize;
  initUserList();
}

const handleCurrentChange=(pageNum)=>{
  queryForm.value.pageNum=pageNum;
  initUserList();
}

const handleDialogValue=(userId)=>{
  if(userId){
    id.value=userId;
    dialogTitle.value="用户修改"
  }else{
    dialogTitle.value="用户添加"
  }
  dialogVisible.value=true
}
const statusChangeHandle=async (row)=>{
  let res=await requestUtil.get("/member/sys/user/updateStatus/"+row.id+"/status/"+row.status);
  if(res.data.code==200){
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
  }else{
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
    initUserList();
  }
}

</script>
